package net.sunofbeach.blog.services.impl;

import net.sunofbeach.blog.dao.NavigationCategoryDao;
import net.sunofbeach.blog.pojo.NavigationCategory;
import net.sunofbeach.blog.pojo.SobUser;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.INavigationCategoryService;
import net.sunofbeach.blog.services.IUserService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NavigationCategoryServiceImpl extends BaseService implements INavigationCategoryService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private NavigationCategoryDao navigationCategoryDao;

    @Autowired
    private IUserService userService;


    @Override
    public ResponseResult addNavigationCategory(NavigationCategory navigationCategory) {
        //先检查数据
        //必须有的数据：
        //分类名称，分类的拼音，顺序，描述
        if (TextUtils.isEmpty(navigationCategory.getName())) {
            return ResponseResult.FAILED("分类名称不可以为空");
        }
        if (TextUtils.isEmpty(navigationCategory.getPinyin())) {
            return ResponseResult.FAILED("分类拼音不可以为空");
        }
        if (TextUtils.isEmpty(navigationCategory.getDescription())) {
            return ResponseResult.FAILED("分类描述不可以为空");
        }

        //补全数据
        navigationCategory.setId(idWorker.nextId() + "");
        navigationCategory.setStatus("1");
        navigationCategory.setCreateTime(new Date());
        navigationCategory.setUpdateTime(new Date());
        //保存数据
        navigationCategoryDao.save(navigationCategory);
        //返回结果
        return ResponseResult.SUCCESS("添加分类成功");
    }

    @Override
    public ResponseResult getNavigationCategory(String navigationCategoryId) {
        NavigationCategory navigationCategory = navigationCategoryDao.findOneById(navigationCategoryId);
        if (navigationCategory == null) {
            return ResponseResult.FAILED("分类不存在.");
        }
        return ResponseResult.SUCCESS("获取分类成功.").setData(navigationCategory);
    }

    @Override
    public ResponseResult listNavigationCategories() {
        //参数检查

        //创建条件
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");
        //Sort sort = new Sort(Sort.Direction.ASC, "order");

        //判断用户角色，普通用户/未登录用户，只能获取到正常的category
        //管理员账户，可以拿到所有的分类
        SobUser sobUser = userService.checkSobUser();
        List<NavigationCategory> navigationCategories;
        if (sobUser == null || !Constants.User.ROLE_ADMIN.equals(sobUser.getRoles())) {
            //只能获取到正常的category
            navigationCategories = navigationCategoryDao.listNavigationCategoriesByState("1");
        }else {

            navigationCategories = navigationCategoryDao.findAll(sort);
        }
        //查询
        //返回结果
        return ResponseResult.SUCCESS("获取分类列表成功").setData(navigationCategories);
    }

    @Override
    public ResponseResult updateNavigationCategory(String navigationCategoryId, NavigationCategory navigationCategory) {
        //第一步是找出来
        NavigationCategory navigationCategoryFromDb = navigationCategoryDao.findOneById(navigationCategoryId);
        if (navigationCategoryFromDb == null) {
            return ResponseResult.FAILED("分类不存在");
        }
        //第二部时对内容判断，有些字段是不可以为空的
        String name = navigationCategory.getName();
        if (!TextUtils.isEmpty(name)) {
            navigationCategoryFromDb.setName(name);
        }

        String pinyin = navigationCategory.getPinyin();
        if (!TextUtils.isEmpty(pinyin)) {
            navigationCategoryFromDb.setPinyin(pinyin);
        }

        String description = navigationCategory.getDescription();
        if (!TextUtils.isEmpty(description)) {
            navigationCategoryFromDb.setDescription(description);
        }
        navigationCategoryFromDb.setStatus(navigationCategory.getStatus());
        navigationCategoryFromDb.setOrder(navigationCategory.getOrder());
        navigationCategoryFromDb.setUpdateTime(new Date());
        //第三步是保存数据
        navigationCategoryDao.save(navigationCategoryFromDb);
        //返回结果
        return ResponseResult.SUCCESS("分类更新成功");
    }

    @Override
    public ResponseResult deleteNavigationCategory(String navigationCategoryId) {
        int result = navigationCategoryDao.deleteAllById(navigationCategoryId);

        if (result == 0) {
            return ResponseResult.FAILED("该分类不存在");
        }
        return ResponseResult.SUCCESS("删除分类成功.");
    }
}
